/**
 * 荣誉Api
 * 获取荣誉、战绩等数据
 */
const db = require('../../config/db')

/**
 * 获取荣誉
 * @Param: 学院、运动
 */
exports.getHonor = async(req, res) => {
    try {
        const {college, sport} = req.body
        console.log("college: ", college)
        console.log("sport: ", sport)
        const sql = 'SELECT * FROM college_sport WHERE college = ? AND sport = ?';
        const [rows] = await db.query(sql, [college, sport]);
        // todo 判断rows长度
        if(rows.length == 0) {
            return res.status(200).json({
                champion: 0,     
                secondWinner: 0,
                thirdWinner: 0,
                honorMoment: "　　暂未录入。", // 荣誉时刻
                introduction: "　　暂未录入。" // 球队介绍
            })    
        }
        const row = rows[0]
        res.status(200).json({
            id: row.id,
            champion: row.champion,
            secondWinner: row.second_winner,
            thirdWinner: row.third_winner,
            honorMoment: row.honor_moment,
            introduction: row.introduction
        })    
    } catch (err) {
        console.log("getHonor报错: ", err)
        res.status(500).json({ error: '获取荣誉报错!' })
    }
}

/**
 * 获得战绩数据
 */
exports.getFight = async(req, res) => {
    try {
        const { college, sport } = req.body;
        const sql = `
            SELECT * FROM fight
            WHERE sport = ?
            AND (home_team = ? OR visit_team = ?)
        `;
        const params = [sport, college, college];
        const [rows] = await db.query(sql, params);

        res.status(200).json(
            rows.map(row => {
                let resultText = "";
                switch (row.result) {
                    case 1:
                        if(row.home_team == college)
                            resultText = "胜";
                        else
                            resultText = "负"
                        break;
                    case 0:
                        resultText = "平";
                        break;
                    case 2:
                        if(row.visit_team == college) 
                            resultText = "胜";
                        else
                            resultText = "负";
                        break;
                    default:
                        resultText = "未知";
                }
                console.log("row.result: ", row.result)
                console.log("row.homeTeam: ", row.home_team)
                console.log("row.visitTeam: ", row.visit_team)
                console.log("resultText: ", resultText)

                return {
                    id: row.id,
                    sport: row.sport,
                    gameName: row.game_name,
                    gameType: row.game_type,
                    homeTeam: row.home_team,
                    visitTeam: row.visit_team,
                    fraction: row.fraction,
                    result: resultText,      // 原始数值
                    time: row.time
                };
            })
        );
    } catch (err) {
        console.log("err: ", err);
        res.status(500).json({ error: "服务器异常" });
    }
}